package cn.ihrm.sass.response;

import cn.ihrm.sass.entity.system.Permission;
import cn.ihrm.sass.entity.system.Role;
import cn.ihrm.sass.entity.system.User;
import lombok.Getter;
import lombok.Setter;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
 * 构造用户信息返对象
 *
 * @author Mxb
 * @version 1.0
 * @date 2020/7/5 10:52
 */
@Getter
@Setter
public class ProfileResponse {
    private String mobile;

    private String username;

    private String company;


    private Map<String, Object> roles = new HashMap<>();

    public ProfileResponse(User user) {
        this.mobile = user.getMobile();
        this.username = user.getUsername();
        this.company = user.getCompanyName();

        Set<String> menus = new HashSet<>();
        Set<String> points = new HashSet<>();
        Set<String> apis = new HashSet<>();

        Set<Role> roles = user.getRoles();
        for (Role role : roles) {
            Set<Permission> permissions = role.getPermissions();
            for (Permission perm : permissions) {
                String code = perm.getCode();
                if (perm.getType() == 1) {
                    menus.add(code);
                } else if (perm.getType() == 2) {
                    points.add(code);
                } else {
                    apis.add(code);
                }
            }
        }
        this.roles.put("munus", menus);
        this.roles.put("points", points);
        this.roles.put("apis", apis);
    }
}
